//
//  PopMenu.m
//  CC
//
//  Created by Calvin on 4/27/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "PopMenu.h"
#import "UIImage+Extension.h"

@interface PopMenu()
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic,strong) UIImageView *container;
@property (nonatomic,strong) UIButton *cover;
@end

@implementation PopMenu
-(void)setDimBackground:(BOOL)dimBackground
{
    _dimBackground=dimBackground;
    if (self.isdimBackground) {
        self.cover.backgroundColor = [UIColor blackColor];
        self.cover.alpha=0.2;
    }
    else
    {
        self.cover.backgroundColor =[UIColor clearColor];
        self.cover.alpha=1.0;
    }
}

-(void)setArrowPosition:(PopMenuArrowPosition)arrowPosition
{
    _arrowPosition =arrowPosition;
    switch (_arrowPosition) {
        case PopMenuArrowPositionCenter:
            self.container.image=[UIImage resizedImage:@"popover_background"];
            break;
        case PopMenuArrowPositionLeft:
            self.container.image=[UIImage resizedImage:@"popover_background_left"];
            break;
        case PopMenuArrowPositionRight:
            self.container.image=[UIImage resizedImage:@"popover_background_right"];
            break;
    }
}

-(id)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        UIButton *cover =[[UIButton alloc] init];
        cover.backgroundColor =[UIColor clearColor];
        [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cover];
        self.cover = cover;
        
        UIImageView *container = [[UIImageView alloc] init];
        container.userInteractionEnabled=YES;
        CGRect frame = container.frame;
        frame.size = CGSizeMake(200, 100);
        container.frame =frame;
        container.image =[UIImage resizedImage:@"popover_background"];
        [self addSubview:container];
        self.container=container;
    }
    
    return self;
}

-(instancetype)initWithContentView:(UIView *)contentView
{
    if (self=[super init]) {
        self.contentView =contentView;
    }
    
    return self;
}

+(instancetype)popMenuWithContentView:(UIView *)contentView
{
    return [[self alloc] initWithContentView:contentView];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.cover.frame=self.bounds;
}


#pragma make interior method
-(void)coverClick
{
    [self dismiss];
}

#pragma make public method
-(void)setBackground:(UIImage *)background
{
    self.container.image=background;
}

-(void)showInRect:(CGRect)rect
{
    UIWindow *window =[UIApplication sharedApplication].keyWindow;
    self.frame=window.frame;
    [window addSubview:self];
    
    self.container.frame=rect;
    [self.container addSubview:self.contentView];
    
    CGFloat topMargin = 12;
    CGFloat leftMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat bottomMargin = 8;
    self.contentView.frame = CGRectMake(leftMargin, topMargin, self.container.frame.size.width- leftMargin - rightMargin, self.container.frame.size.height- topMargin - bottomMargin);
    
}

-(void)dismiss
{
    if ([self.popMenudelegate respondsToSelector:@selector(popMenuDidDismissed:)]) {
        [self.popMenudelegate popMenuDidDismissed:self];
    }
    [self removeFromSuperview];
}
@end
